package com.dzpk.gameserver.util;

import com.dzpk.gameserver.action.CommandEnum;
import com.dzpk.gameserver.conf.SystemConfig;
import com.dzpk.gameserver.module.player.PlayerControler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import somnus.cache.ICache;
import somnus.net.DefaultHandler;
import somnus.net.SocketServer;
import somnus.schedule.ScheduleManager;
import somnus.task.ITask;

import java.util.concurrent.LinkedBlockingQueue;

public class BeanControler implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public static SocketServer getGameServer() {
        return (SocketServer) applicationContext.getBean("gameServer");
    }


    public static AsynThreadControler getAsynThreadControler() {
        return (AsynThreadControler) applicationContext.getBean("asynThreadControler");
    }

    public static AsynThreadControler getMoneyThreadControler() {
        return (AsynThreadControler) applicationContext.getBean("moneyThreadControler");
    }

    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    public static Object getC2SAction(CommandEnum commandEnum) {
        return applicationContext.getBean("c2s_" + commandEnum.getId());
    }

    public static AsynThreadControler getCacheExecutor() {
        return (AsynThreadControler) applicationContext.getBean("cacheThread");
    }

    // public static CacheControler getCacheControler() {
    // return (CacheControler) applicationContext.getBean("cacheControler");
    // }

    public static ApplicationContext getContext() {
        return applicationContext;
    }


    public static ICache getRoleCache() {
        return (ICache) applicationContext.getBean("gameCache");
    }


    public static ScheduleManager getScheduleManager() {
        return (ScheduleManager) applicationContext.getBean("scheduleManager");
    }

    public static SystemConfig getSystemConfig() {
        return (SystemConfig) applicationContext.getBean("systemConfig");
    }


    public static AsyncGetCache getAsynCache() {
        return (AsyncGetCache) applicationContext.getBean("asyncGetCache");
    }

    public static OrderIdGenerator getOrderIdGenrator() {
        return ((OrderIdGenerator) applicationContext.getBean("orderIdGenerator"));
    }

    public static IDGenerator getIDGenrator() {
        return ((IDGenerator) applicationContext.getBean("iDGenerator"));
    }


    public static IDGenerator getReportIDGenerator() {
        return ((IDGenerator) applicationContext.getBean("reportIDGenerator"));
    }



    @Override
    public void setApplicationContext(ApplicationContext ctx) {
        BeanControler.applicationContext = ctx;
    }


    public static LogicJob getLogicJob() {
        return (LogicJob) applicationContext.getBean("logicJob");
    }


    public static void addTask(ITask task) {
        ((DefaultHandler) applicationContext.getBean("gameHandler")).addTask(task);
    }

    public static LinkedBlockingQueue<ITask> getTaskQueue() {
        return ((DefaultHandler) applicationContext.getBean("gameHandler")).getSynTasks();
    }

}
